﻿using MyUAMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MyUAMS.BLL
{
    
    public class AdminServer
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        public List<AP_Table> GetOneAP(int BR)
        {
            if (BR == 0)
            {
                return (from p in db.AP_Table
                        where p.Status == "一审中" && (p.APname == "出库" || p.APname == "入库")
                        orderby p.Date
                        select p).ToList();
            }
            else
            {
                return (from p in db.AP_Table
                        where (p.Status == "一审中") && (p.APname == "借出" || p.APname == "还入")
                        orderby p.Date
                        select p).ToList();
            }
        }
        public List<AP_Table> GetTweAP(int BR)
        {
            if (BR == 0)
            {
                return (from p in db.AP_Table
                        where (p.Status == "二审中") && (p.APname == "出库" || p.APname == "入库")
                        orderby p.Date
                        select p).ToList();
            }
            else
            {
                return (from p in db.AP_Table
                        where (p.Status == "二审中") && (p.APname == "借出" || p.APname == "还入")
                        orderby p.Date
                        select p).ToList();
            }
        }
        public List<AP_Table> GetThreeAP(int BR)
        {
            if (BR == 0)
            {
                return (from p in db.AP_Table
                        where (p.Status == "三审中") && (p.APname == "出库" || p.APname == "入库")
                        orderby p.Date
                        select p).ToList();
            }
            else
            {
                return (from p in db.AP_Table
                        where (p.Status == "三审中") && (p.APname == "借出" || p.APname == "还入")
                        orderby p.Date
                        select p).ToList();
            }
        }
        public List<AP_Table> GetAllAP(int BR)
        {
            if(BR == 0)
            {
                return (from p in db.AP_Table
                        where (p.Status == "一审中" || p.Status == "二审中" || p.Status == "三审中") && (p.APname == "出库" || p.APname=="入库")
                        orderby p.Date
                        select p).ToList();
            }
            else
            {
                return (from p in db.AP_Table
                        where (p.Status == "一审中" || p.Status == "二审中" || p.Status == "三审中") && (p.APname == "借出" || p.APname == "还入")
                        orderby p.Date
                        select p).ToList();
            }
        }

        public void UpdateAP(int apid, int aid, string type, int amount, int step, int apuid, string apuname, string status, int uid)
        {
            AP_Table ap = db.AP_Table.FirstOrDefault(p => p.APid == apid);
            if (ap == null)
            {
                return;
            }
            else
            {
                if (status == "打回")
                {
                    ap.Status = "打回";
                    ap.Onedate = DateTime.Now;
                    ap.APuid = apuid;
                    ap.APuname = apuname;
                    if (type == "借出" || type == "还入")
                    {
                        BR_Table br = db.BR_Table.FirstOrDefault(c => c.APid == apid);
                        br.Status = "打回";
                    }
                }
                else
                {
                    switch (step)
                    {
                        case 1:
                            ap.Step = 2;
                            ap.APuid = apuid;
                            ap.APuname = apuname;
                            ap.Onedate = DateTime.Now;
                            ap.Status = "二审中";
                            break;
                        case 2:
                            ap.Step = 3;
                            ap.Twedate = DateTime.Now;
                            ap.Status = "三审中";
                            break;
                        case 3:
                            Asset_Table asset = db.Asset_Table.FirstOrDefault(c => c.Aid == aid);
                            if (asset != null)
                            {
                                if (type == "入库" || type == "还入")
                                {
                                    asset.Aamount += amount;
                                    if (type == "还入" )
                                    {
                                        if (asset.status == "借出")
                                        {
                                            
                                        }
                                        else
                                        {
                                            asset.status = "正常";
                                        }
                                        BR_Table br = db.BR_Table.FirstOrDefault(c =>c.APid == apid);
                                        br.Status = "审核成功";
                                    }
                                }
                                else if (type == "出库" || type == "借出")
                                {
                                    asset.Aamount -= amount;
                                    if (type == "借出")
                                    {
                                        if (asset.status == "正常" || asset.status == "")
                                        {
                                            asset.status = "借出";
                                        }
                                        else
                                        {
                                            
                                        }
                                        BR_Table br = db.BR_Table.FirstOrDefault(c => c.APid == apid);
                                        br.Status = "审核成功";

                                    }
                                }
                            }
                            ap.Threedate = DateTime.Now;
                            ap.Status = "审核完成";
                            break;
                    }
                }
            }

            

            db.SubmitChanges();
        }

    }
}
